﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BuyPanel : MonoBehaviour
{
    private GameID TheGameId;
    private string NowGamer;
    private int Number;
    private int price;
    public GameObject AudioGameObject;
    public int[] prices;
    public GameObject Panel_ChangeMainMusic;
    public GameObject O_Panel;
    public AudioClip originMusic;

    public float getMoney()
    {
        return PlayerPrefs.GetFloat(NowGamer + "Money");
    }

    public void setMoney(float money)
    {
        PlayerPrefs.SetFloat(NowGamer + "Money", money);
    }

    public void setMusic(int no)
    {
        PlayerPrefs.SetInt(NowGamer + "Music", no);
    }

    public int getMusic()
    {
        return PlayerPrefs.GetInt(NowGamer + "Music");
    }

    public int getShop()
    {
        return PlayerPrefs.GetInt(NowGamer + "Shop");
    }

    public void setShop(int Shop)
    {
        PlayerPrefs.SetInt(NowGamer + "Shop", Shop);
    }
    private bool Purchased()
    {
        TheGameId = GameObject.Find("Main Camera").GetComponent<GameID>();
        int a = getShop();
        int b = 1 << (Number - 1);
        return (a&b)!=0;
    }
    public void init(int Number)
    {
        this.Number = Number;
        this.price = prices[Number - 1];
        if (Purchased())
        {
            Panel_ChangeMainMusic.SetActive(true);
        }
        else OpenWindow();
    }

    public void Buy()
    {
        if (getMoney() >= price&&!Purchased())
        {
            setMoney(getMoney() - price);
            setShop(getShop() | (1 << (Number - 1)));
            CloseWindow();
        }
        else
        {
            NoMoneyOrBought();
        }
    }

    void Awake()
    {
        TheGameId = GameObject.Find("Main Camera").GetComponent<GameID>();
        NowGamer = TheGameId.nowGamer;

    }

    public void OpenWindow()
    {
        transform.gameObject.SetActive(true);
    }

    public void CloseWindow()
    {
        transform.gameObject.SetActive(false);
    }

    public void changeMusic()
    {
        setMusic(Number);
        MusicSetted();
    }

    public void TryChangeMusic()
    {
        AudioGameObject.GetComponent<Shop>().TryMusic(Number);
    }

    private void NoMoneyOrBought()
    {
        transform.Find("BoughtPanel").gameObject.SetActive(true);
    }

    public void ReturnPanel()
    {
        transform.Find("BoughtPanel").gameObject.SetActive(false);
    }

    public void MusicSetted()
    {
        Panel_ChangeMainMusic.SetActive(false);
    }

    public void originPanelOpen()
    {
        O_Panel.gameObject.SetActive(true);
    }

    public void originPanelClose()
    {
        O_Panel.gameObject.SetActive(false);
    }

    public void originMusicTry()
    {
        GameObject.Find("Canvas").GetComponent<AudioSource>().clip = originMusic;
        GameObject.Find("Canvas").GetComponent<AudioSource>().Play();
    }

    public void originMusicSet()
    {
        TheGameId = GameObject.Find("Main Camera").GetComponent<GameID>();
        setMusic(0);
        originPanelClose();
    }

}
